<?php
namespace Admin\Controller;

class UserController extends AdminController {
    /**
     * [index 传输用户数据]
     * @return [二维数组] [用户信息]
     */
    public function index(){
        if (empty($_GET['name'])){
            $user = M('user_info');
            $count = $user->table('baidu_user_info info,baidu_user user')->where('user.id = info.uid')->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $this->lastSuffix = false;
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('first','首页');
            $Page->setConfig('last','尾页');
            $show = $Page->show();// 分页显示输出

            $list = $user->field('user.id,user.name,user.email,user.phone,user.time,info.sex,info.status,info.nickname,info.gold,info.exp')->table('baidu_user_info info,baidu_user user')->where('user.id = info.uid')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
                
            if (empty($list)) {
                $list = 0;
            }
            $this->assign('list',$list);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
        }else{
            $user = M('user_info');
            $name = $_GET['name'];
            // $name['user.name'] = array('LIKE','%'.$_POST['name'].'%');
            $count = $user->table('baidu_user_info info,baidu_user user')->where("user.id = info.uid AND user.name LIKE '%".$name."%'")->count();// 查询满足要求的总记录数
            $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->parameter['name'] = $name;
            $this->lastSuffix = false;
            $Page->setConfig('prev','上一页');
            $Page->setConfig('next','下一页');
            $Page->setConfig('first','首页');
            $Page->setConfig('last','尾页');
            $show = $Page->show();// 分页显示输出

            $list = $user->field('user.id,user.name,user.email,user.phone,user.time,info.sex,info.status,info.nickname,info.gold,info.exp')->table('baidu_user_info info,baidu_user user')->where("user.id = info.uid AND user.name LIKE '%".$name."%'")->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
                
            if (empty($list)) {
                $list = 0;
            }
            $this->assign('list',$list);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
        }
        
        $this->display('User/index');
    }



    public function add(){
        $_POST['time'] = date('Y-m-d h:i:s');
        // $_POST['password'] = md5($_POST['password']);
        $list = $_POST;
        $user = D('user');


        if(!$user->create()){
            $this->error($user->getError());
            exit;
        }else{
            $result = $user->add(); // 写入数据到数据库
            if($result){
            // 如果主键是自动增长型 成功后返回值就是最新插入的值
            $list['uid'] = $result;
            M("user_info")->data($list)->add();
            $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }

        }
    }

    public function info(){
        $id=$_GET['id'];
        $info = M('user_info');
        $user = M('user');
        $info_s = $user->where('id ='.$id)->find();
        $info_m = $info->where('uid ='.$id)->find();
        $this->assign('info_s',$info_s);
        $this->assign('info_m',$info_m);
        $exp = M('experience')->where('uid = '.$id)->select();
        $this->assign('exp',$exp);
        $this->display('User/info');
    }
    public function nospeak(){
        $id = $_GET['id'];
        $user = M('user_info');
        $data['status'] = '1';
        $user->where('uid='.$id)->save($data); // 根据条件更新记录
        $this->ajaxReturn($id);
    }

    public function normal(){
        $id = $_GET['id'];
        $user = M('user_info');
        $data['status'] = '0';
        $user->where('uid='.$id)->save($data); // 根据条件更新记录
        $this->ajaxReturn($id);
    }

    public function kill(){
        $id = $_GET['id'];
        $user = M('user_info');
        $data['status'] = '2';
        $user->where('uid='.$id)->save($data); // 根据条件更新记录
        $this->ajaxReturn($id);
    }


    public function del(){
        $id = $_GET['id'];
        $user = M('user');
        $user->delete($id);
        $user_info = M("user_info"); // 实例化user_info对象
        $user_info->where('uid='.$id)->delete(); // 删除uid为$id的用户数据
        $this->ajaxReturn($id);
    }

    public function edit(){
        $id = $_GET['id'];
        $info = M('user_info');
        $user = M('user');
        $info_s = $user->where('id ='.$id)->find();
        $info_m = $info->where('uid ='.$id)->find();
        $info_m['email'] = $info_s['email'];
        $info_m['phone'] = $info_s['phone'];
        $info_m['time'] = $info_s['time'];
        $info_m['name'] = $info_s['name'];
        $this->ajaxReturn($info_m);
    }

    public function update(){
        //判断 有无传递POST
        if (empty($_POST)) {
            $this->error('请完善数据!');
            exit;
        }
        // $_POST['password'] = md5($_POST['password']);
        foreach ($_POST as $key => $value) {
            if ($key == 'password' && empty($value)) {
                unset($_POST[$key]);
            }
        }

        $_POST = $_POST;
        $list = $_POST;
        $id = $_POST['id'];

        //初始化
        $model = D('User');
        if(!$model->create()){
            $this->error($model->getError());
            exit;
        }
        if(!D('user_info')->create($list)){
            $this->error(D('user_info')->getError());
            exit;
        }

        $model->where('id='.$id)->save($list);
        D('user_info')->where('uid='.$id)->save($list);
        $this->success('修改成功');
    }

}